import MsgComponent from '../components/msg.vue'

const Msg = {}

Msg.install = (Vue) => {
  const vue = Vue;
  const MsgClass = vue.extend(MsgComponent)
  const instance = new MsgClass();
  instance.$mount(document.createElement('div'));
  document.body.appendChild(instance.$el);

  let timer;

  const MsgMain = {
    show_message(txt, type) {
      clearTimeout(timer);
      timer = setTimeout(() => {
        instance.visible = false
      }, 1500);
      instance.txt = txt;
      instance.type = type;
      instance.visible = true;
    },

    success(txt, type = 'success') {
      this.show_message(txt, type)
    },

    error(txt, type = 'error') {
      this.show_message(txt, type)
    }
  }

  vue.prototype.$Message = MsgMain
}

export default Msg;